package com.elecbook.content.service;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.elecbook.base.dto.PageDTO;
import com.elecbook.base.constant.ContributeJudgeStateConstant;
import com.elecbook.base.enums.OperationType;
import com.elecbook.base.exception.BaseInfoException;
import com.elecbook.base.util.SecurityUtil;
import com.elecbook.base.util.TimeFillUtil;
import com.elecbook.base.util.VerificationUtil;
import com.elecbook.content.mapper.ContributeMapper;
import com.elecbook.content.mapper.ContributejudgeMapper;
import com.elecbook.content.model.dto.ContributePQuery;
import com.elecbook.content.model.dto.ContributePageRequestDTO;
import com.elecbook.content.model.dto.ContributeSubmitDTO;
import com.elecbook.content.model.po.Contribute;
import com.elecbook.content.model.po.Contributejudge;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Z-ken
 * @since 2024/10/24 19:39:39
 */
@Service
public class ContributeServiceImpl extends ServiceImpl<ContributeMapper, Contribute> implements ContributeService {

    @Autowired
    ContributeMapper contributeMapper;
    @Autowired
    ContributejudgeMapper contributejudgeMapper;

    @Override
    public void deleteContributeById(Contribute contribute) {
        Contribute contributeDb = contributeMapper.selectById(contribute.getId());
        if (!SecurityUtil.hasAuthority("ADMIN")) {
            VerificationUtil.verify(contributeDb, Contribute::getUserId);
        }
        baseMapper.deleteById(contribute.getId());
    }

    @Override
    public PageDTO<Contribute> queryContributePage(ContributePageRequestDTO contributePageRequestDTO) {
        // 1.添加排序规则
        // 使用内置的函数转换并且 设置规则
        List<OrderItem> orderItems = contributePageRequestDTO.toMpOrderItems();

        // 获取分页对象和排序情况
        Page<Contribute> page = contributePageRequestDTO.toMpPageDefaultSortByIdAsc(orderItems.toArray(new OrderItem[0]));
        /*
        查询, 自定义的分页查询
         */
        Page<Contribute> p = baseMapper.selectPageVO(page, contributePageRequestDTO);

        // 获取数据信息
        return PageDTO.of(p, Contribute.class);
    }

    @Override
    public void updateContributeByID(ContributeSubmitDTO contributeSubmitDTO) {
        Contribute contribute = contributeMapper.selectById(contributeSubmitDTO.getId());
        // 检验是否为本人
        if (!SecurityUtil.hasAuthority("ADMIN")) {
            VerificationUtil.verify(contribute, Contribute::getUserId);
        }
        BeanUtils.copyProperties(contributeSubmitDTO, contribute);
        baseMapper.updateById(contribute);
    }

    @Override
    public Contribute getContributeById(Integer contributeId) {
        return baseMapper.selectById(contributeId);
    }

    @Transactional
    @Override
    public Contribute submitContribute(ContributeSubmitDTO dto) {
        // 拷贝
        Contribute contribute = new Contribute();
        BeanUtils.copyProperties(dto, contribute);
        contribute.setUserId(SecurityUtil.getCurrentId());
        // insert的时候，将id设置为null
        contribute.setId(null);
        TimeFillUtil.fillTimeAndCreateUser(contribute, OperationType.INSERT);

        // TODO 提交到审核表单中
        if (StringUtils.isBlank(contribute.getTitle())) {
            throw new BaseInfoException("不可以提交空名称的稿件");
        }

        contribute.setUserId(SecurityUtil.getCurrentId());
        if (SecurityUtil.hasAuthority("ADMIN")) {
            contribute.setJudgeStatus(ContributeJudgeStateConstant.ACCEPT);
        } else {
            contribute.setJudgeStatus(ContributeJudgeStateConstant.SUBMIT);
        }
        contributeMapper.insert(contribute);
        // 添加审核项目
        Contributejudge contributejudge = new Contributejudge();
        BeanUtils.copyProperties(contribute, contributejudge);
        contributejudge.setContributeId(contribute.getId());
        contributejudgeMapper.insert(contributejudge);

        return contribute;
    }

    /**
     * 基础分页请求--管理员
     */
    @Override
    public PageDTO<Contribute> pageQuery(ContributePQuery query) {
        // 1.添加排序规则
        // 使用内置的函数转换并且 设置规则-这里为了方便扩展和使用先进行创建
        List<OrderItem> orderItems = query.toMpOrderItems();

        // 获取分页对象和排序情况
        Page<Contribute> page = query.toMpPageDefaultSortByIdAsc(orderItems.toArray(new OrderItem[0]));
    /*
    查询
    */
        Page<Contribute> p = baseMapper.selectPageEntity(page, query);
        // 获取数据信息
        return PageDTO.of(p, Contribute.class);
    }


    @Override
    public Contribute saveContribute(Contribute contribute) {
        contribute.setId(null);
        baseMapper.insert(contribute);
        return contribute;
    }

    @Override
    public Contribute updateContributeById(Contribute contribute) {
        baseMapper.updateById(contribute);
        return contribute;
    }

}
